using RxjhServer.DbClss;
using System;
using System.Collections.Generic;
using System.Timers;

namespace RxjhServer
{
	public class 帮派战_门战
	{
		private object AsyncLock = new object();

		private System.Timers.Timer 门主申请记时器;

		private System.Timers.Timer 准备记时器;

		private System.Timers.Timer 开始对战记时器;

		private System.Timers.Timer 结束对战记时器;

		private System.Timers.Timer 发奖励记时器;

		private System.Timers.Timer 刷怪物记时器;

		private DateTime 申请时间;

		private DateTime 准备时间;

		private int 时间计数;

		private int 获得奖励人数;

		public 帮派战_门战()
		{
			try
			{
				World.门战进程 = 1;
				World.帮战list = new Dictionary<int, 帮战Class>();
				门主申请记时器 = new System.Timers.Timer(60000.0);
				门主申请记时器.Elapsed += 门主申请记时器结束事件;
				门主申请记时器.Enabled = true;
				门主申请记时器.AutoReset = true;
				申请时间 = DateTime.Now.AddMinutes(5.0);
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "门战 准备记时器结束事件 出错：" + ex?.ToString());
			}
		}

		public void 门主申请记时器结束事件(object source, ElapsedEventArgs e)
		{
			try
			{
				int num = (int)申请时间.Subtract(DateTime.Now).TotalSeconds;
				World.conn.发送("全服公告|10|帮战|离申请还剩下" + ((num + 59) / 60).ToString() + "分钟，请帮派帮主召集帮派成员到1线银币广场[门战传送]处申请");
				if (num <= 0)
				{
					World.门战进程 = 1;
					if (World.帮战list.Count < 2)
					{
						foreach (帮战Class value in World.帮战list.Values)
						{
							using (new Lock(value.申请人物列表, "帮战申请人物列表"))
							{
								foreach (Players value2 in value.申请人物列表.Values)
								{
									value2.系统提示("由于没有匹配的帮派无法进行门战", 10, "系统提示");
								}
							}
						}
						Dispose();
					}
					else
					{
						门主申请记时器.Enabled = false;
						门主申请记时器.Close();
						门主申请记时器.Dispose();
						foreach (帮战Class value3 in World.帮战list.Values)
						{
							using (new Lock(value3.申请人物列表, "帮战申请人物列表"))
							{
								foreach (Players value4 in value3.申请人物列表.Values)
								{
									value4.系统公告("门战配对成功,请做好战斗准备,1分钟后开始门战");
								}
							}
						}
						准备时间 = DateTime.Now.AddMinutes(2.0);
						准备记时器 = new System.Timers.Timer(60000.0);
						准备记时器.Elapsed += 准备记时器结束事件;
						准备记时器.Enabled = true;
						准备记时器.AutoReset = true;
					}
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "门战 申请记时器结束事件 出错：" + ex?.ToString());
			}
		}

		public void 准备记时器结束事件(object source, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "准备记时器结束事件");
			}
			try
			{
				int num = (int)准备时间.Subtract(DateTime.Now).TotalSeconds;
				if (num <= 0)
				{
					World.门战进程 = 2;
					准备记时器.Enabled = false;
					准备记时器.Close();
					准备记时器.Dispose();
					申请时间 = DateTime.Now.AddMinutes(15.0);
					开始对战记时器 = new System.Timers.Timer(60000.0);
					开始对战记时器.Elapsed += 开始对战记时器结束事件;
					开始对战记时器.Enabled = true;
					开始对战记时器.AutoReset = true;
					int num2 = -458;
					int num3 = -6;
					foreach (帮战Class value in World.帮战list.Values)
					{
						using (new Lock(value.申请人物列表, "帮战申请人物列表"))
						{
							foreach (Players value2 in value.申请人物列表.Values)
							{
								value2.移动(num2, num3, 15f, World.门战地图);
								value2.系统公告("门战开始,请尽快击杀其他门派成员,最后积分最多的门派将获得丰厚奖励");
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "门战 准备记时器结束事件 出错：" + ex?.ToString());
			}
		}

		public void 开始对战记时器结束事件(object source, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "开始对战记时器结束事件");
			}
			try
			{
				if (World.门战进程 == 2)
				{
					int num = (int)申请时间.Subtract(DateTime.Now).TotalSeconds;
					if (num <= 0)
					{
						Form1.WriteLine(1, "门战：" + num.ToString() + "胜利个数:" + World.帮战list.Count.ToString());
						World.门战进程 = 3;
						num = 0;
					}
					时间计数 = num;
					if (World.帮战list.Count > 0)
					{
						foreach (帮战Class value in World.帮战list.Values)
						{
							if (value.申请人物列表.Count < 1)
							{
								foreach (Players value2 in value.申请人物列表.Values)
								{
									value2.系统提示("由于您所在的帮派参战人数不足3人，视为自动放弃门战！", 10, "系统信息");
									value2.移动(0f, 0f, 15f, 1201);
								}
								World.帮战list.Remove(value.申请帮派ID);
							}
						}
						foreach (Players value3 in World.allConnectedChars.Values)
						{
							if (value3.人物坐标_地图 == World.门战地图)
							{
								value3.系统提示(((num + 60) / 60).ToString() + "分钟后帮战结束，为帮派做贡献的时刻到了，请奋勇杀敌不畏生死", 10, "系统信息");
								foreach (帮战Class value4 in World.帮战list.Values)
								{
									value3.系统提示("[" + value4.申请帮派名称 + "]得分[" + value4.帮派分数.ToString() + "]", 21, "门战成绩");
								}
							}
						}
					}
					if (时间计数 <= 0 || World.帮战list.Count < 2)
					{
						开始对战记时器.Enabled = false;
						开始对战记时器.Close();
						开始对战记时器.Dispose();
						发奖励记时器 = new System.Timers.Timer(60000.0);
						发奖励记时器.Elapsed += 门战最后发送奖励;
						发奖励记时器.Enabled = true;
						发奖励记时器.AutoReset = true;
						Form1.WriteLine(1, "门战：" + 时间计数.ToString() + "胜利个数:" + World.帮战list.Count.ToString());
					}
				}
			}
			catch (Exception ex)
			{
				开始对战记时器.Enabled = false;
				开始对战记时器.Close();
				开始对战记时器.Dispose();
				发奖励记时器 = new System.Timers.Timer(60000.0);
				发奖励记时器.Elapsed += 门战最后发送奖励;
				发奖励记时器.Enabled = true;
				发奖励记时器.AutoReset = true;
				Form1.WriteLine(1, "门战：" + 时间计数.ToString() + "胜利个数:" + World.帮战list.Count.ToString());
				Form1.WriteLine(1, "门战 开始对战记时器结束事件 出错：" + ex?.ToString());
			}
		}

		public void 门战最后发送奖励(object source, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "门战最后发送奖励");
			}
			try
			{
				获得奖励人数 = 20;
				发奖励记时器.Enabled = false;
				发奖励记时器.Close();
				发奖励记时器.Dispose();
				int num = 0;
				foreach (帮战Class value in World.帮战list.Values)
				{
					if (value.帮派分数 > num)
					{
						num = value.帮派分数;
					}
				}
				foreach (帮战Class value2 in World.帮战list.Values)
				{
					if (value2.帮派分数 == num)
					{
						World.conn.发送("全服公告|10|门站|结束！获胜帮派:" + value2.申请帮派名称);
						RxjhClass.帮战最后奖励(value2.申请帮派ID, 1);
						foreach (Players value3 in value2.申请人物列表.Values)
						{
							if (World.获胜门派获得武勋 > 0)
							{
								value3.系统提示("您的帮派胜出系统奖励每人" + World.获胜门派获得武勋.ToString() + "点的武勋", 10, "奖励提示");
								value3.增加属性(0, 0, 0, 0, World.获胜门派获得武勋);
							}
						}
					}
					if (value2.帮派分数 != num)
					{
						RxjhClass.帮战最后奖励(value2.申请帮派ID, -1);
						foreach (Players value4 in value2.申请人物列表.Values)
						{
							if (World.失败门派获得武勋 > 0)
							{
								value4.系统提示("您的帮派失败系统奖励每人" + World.失败门派获得武勋.ToString() + "点的武勋", 10, "奖励提示");
								value4.增加属性(0, 0, 0, 0, World.失败门派获得武勋);
							}
							if (value4.人物坐标_地图 == World.门战地图)
							{
								value4.移动(0f, 0f, 15f, 1201);
							}
						}
					}
				}
				World.conn.发送("全服公告|10|门战|帮战已结束！获胜获得" + World.获胜门派获得武勋.ToString() + "点的武勋，失败获得" + World.失败门派获得武勋.ToString() + "点的武勋。");
				logo.获奖人数Log("门战战斗结束！获胜门派人获得武勋");
				刷怪物记时器 = new System.Timers.Timer(60000.0);
				刷怪物记时器.Elapsed += 刷BOSS事件;
				刷怪物记时器.Enabled = true;
				刷怪物记时器.AutoReset = true;
				准备时间 = DateTime.Now.AddMinutes(60.0);
				结束对战记时器 = new System.Timers.Timer(60000.0);
				结束对战记时器.Elapsed += 门战结束计时器事件;
				结束对战记时器.Enabled = true;
				结束对战记时器.AutoReset = true;
			}
			catch (Exception ex)
			{
				刷怪物记时器 = new System.Timers.Timer(60000.0);
				刷怪物记时器.Elapsed += 刷BOSS事件;
				刷怪物记时器.Enabled = true;
				刷怪物记时器.AutoReset = true;
				准备时间 = DateTime.Now.AddMinutes(60.0);
				结束对战记时器 = new System.Timers.Timer(60000.0);
				结束对战记时器.Elapsed += 门战结束计时器事件;
				结束对战记时器.Enabled = true;
				结束对战记时器.AutoReset = true;
				Form1.WriteLine(1, "门战 门战最后发送奖励 出错：" + ex?.ToString());
			}
		}

		public void 门战随机奖励(Players play, string 申请人, int nIndex)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "门战最后发送奖励-jl");
			}
			try
			{
				string[] array = World.门站随机奖励物品.Split(',');
				if (array.Length != 0 && int.Parse(array[0]) != 0)
				{
					int num = new Random().Next(0, 100);
					if (play.UserName == 申请人)
					{
						num = 100;
					}
					if (num < nIndex)
					{
						play.保存人物的数据();
					}
					else if (play != null)
					{
						try
						{
							int num2 = 18000000;
							if (play.追加状态列表.ContainsKey(9000122))
							{
								追加状态类 追加状态类 = play.追加状态列表[9000122];
								num2 += 追加状态类.FLD_sj;
								追加状态类.时间结束事件();
							}
							获得奖励人数++;
							追加状态类 追加状态类2 = new 追加状态类(play, num2, 9000122, 1);
							play.追加状态列表.Add(追加状态类2.FLD_PID, 追加状态类2);
							play.FLD_人物_追加_经验百分比 += 0.2;
							play.状态效果(BitConverter.GetBytes(9000122), 1, num2);
							play.更新武功和状态();
							play.系统提示("恭喜您获得 门战随机奖励[1.2倍经验状态5小时]", 10, "奖励");
							World.全局提示("门战奖励", 22, "恭喜玩家[" + play.UserName + "]门战随机奖励[1.2倍经验状态5小时]此奖励随机获得！");
							Form1.WriteLine(1, "门战奖励:" + play.UserName);
						}
						catch (Exception)
						{
						}
					}
				}
			}
			catch (Exception ex2)
			{
				Form1.WriteLine(1, "势力战 个人奖励5 出错：" + ex2?.ToString());
			}
		}

		public void 刷BOSS事件(object source, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "门战最后刷BOSS事件");
			}
			try
			{
				刷怪物记时器.Enabled = false;
				刷怪物记时器.Close();
				刷怪物记时器.Dispose();
				string[] array = World.门站BOSS.Split(';');
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i] != null)
					{
						World.AddNpc(int.Parse(array[i].Split(',')[0]), int.Parse(array[i].Split(',')[1]), int.Parse(array[i].Split(',')[2]), World.门战地图);
					}
				}
				Form1.WriteLine(1, "门战最后刷BOSS事件测试：");
			}
			catch (Exception ex)
			{
				Dispose();
				Form1.WriteLine(1, "门战 门战最后刷BOSS事件 出错：" + ex?.ToString());
			}
		}

		public void 门战结束计时器事件(object source, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "门战结束计时器事件");
			}
			try
			{
				int num = (int)准备时间.Subtract(DateTime.Now).TotalSeconds;
				if (num <= 0)
				{
					foreach (Players value in World.allConnectedChars.Values)
					{
						if (value.人物坐标_地图 == World.门战地图)
						{
							value.移动(0f, 0f, 15f, 1201);
						}
					}
					结束对战记时器.Enabled = false;
					结束对战记时器.Close();
					结束对战记时器.Dispose();
					Dispose();
				}
			}
			catch (Exception ex)
			{
				Dispose();
				Form1.WriteLine(1, "门战 门战结束计时器事件 出错：" + ex?.ToString());
			}
		}

		public void Dispose()
		{
			try
			{
				World.门战进程 = 0;
				foreach (Players value in World.allConnectedChars.Values)
				{
					if (value.人物坐标_地图 == World.门战地图)
					{
						value.移动(0f, 0f, 15f, 1201);
					}
				}
				if (门主申请记时器 != null)
				{
					门主申请记时器.Enabled = false;
					门主申请记时器.Close();
					门主申请记时器.Dispose();
				}
				if (准备记时器 != null)
				{
					准备记时器.Enabled = false;
					准备记时器.Close();
					准备记时器.Dispose();
				}
				if (开始对战记时器 != null)
				{
					开始对战记时器.Enabled = false;
					开始对战记时器.Close();
					开始对战记时器.Dispose();
				}
				if (World.帮战list.Count > 0)
				{
					World.帮战list.Clear();
				}
				World.delNpc(World.门战地图, 15136);
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "帮战 Dispose() 出错：" + ex?.ToString());
			}
			World.门战 = null;
		}
	}
}
